data=[]
label=[]
def readdata():
    with open('data.txt',encoding='utf-8') as file:
        j=0
        for i in file:
            data1=i.strip().split(',')
            del data1[0]
            if j==0:
                global label
                label=data1
                j=1
            else:
                data.append(data1)
class Bayeshi():
    def __init__(self):
        pass
    def getclassPrior(self,data:list)->dict:
        '''
        :param data:
        :return:类的先验概率
        '''
        sum=0
        result=dict()
        result_class=dict()
        for i in data:
            lab=i[-1]
            num=result.get(lab,0)
            num+=1
            result[lab]=num
            sum+=1
            divide=result_class.get(lab,[])
            divide.append(i)
            result_class[lab]=divide
        lanmuda=len(result.keys())
        for j in result.keys():
            result[j]=(result[j]+1)/(sum+lanmuda)
        return result,result_class

    def getclassCondition(self,data,label):
        Prior,cond_data=self.getclassPrior(data)
        Condition=dict()
        for i in cond_data.keys():
            classdata=cond_data[i]
            i_mark=Condition.get(i,dict())
            sum=0
            for j in classdata:
                sum+=1
                for k in j:
                    num=i_mark.get(k,0)
                    i_mark[k]=num+1
            lanmuda=len(i_mark.keys())
            for n in i_mark.keys():
                i_mark[n]=(i_mark[n]+1)/(sum+lanmuda)
            Condition[i]=i_mark
        return Prior,Condition
readdata()
watermelon=Bayeshi()
prior,condition=watermelon.getclassCondition(data,label)
print(prior,condition)
test=input('色泽：，根蒂：,敲声：,纹理：,脐部：,触感：,')
data=test.strip().split(',')
result={}
for j in prior.keys():
    result.setdefault(j,prior[j])
for i in data:
    for k in condition.keys():
        result[k]*=condition[k][i]
        print('in',k,'the zhi of i is:',i,'rate:',condition[k][i])

